﻿using MediatR;
using T_Rbac_APi.Application.Command.Quanxian;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.QuanxianHandlers
{
    public class QuanxianAddHandlers : IRequestHandler<QuanxianAddCommand, int>
    {
        private readonly IjobMenuesRep ijobMenuesRep;

        public QuanxianAddHandlers(IjobMenuesRep ijobMenuesRep)
        {
            this.ijobMenuesRep = ijobMenuesRep;
        }

        public async Task<int> Handle(QuanxianAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await ijobMenuesRep.GetvaluesByResult(x => x.Jjid == request.Jjid);
                if (list!=null)
                {
                    await ijobMenuesRep.DeleteRange(list.ToList());
                }
                foreach (var item in request.Mid)
                {
                    var jobmenues = new JobMenus();
                    jobmenues.Jjid = request.Jjid;
                    jobmenues.Mid = item;
                    jobmenues.CreateBy = "小明";
                    jobmenues.CreateDate = DateTime.Now;
                    jobmenues.IsDel=false;
                    await ijobMenuesRep.Add(jobmenues);
                }
                return await Task.FromResult(1);
            }
            catch (Exception)
            {
                return await Task.FromResult(-1);
                throw;
            }
        }
    }
}
